var gulp = require('gulp');
var util = require('gulp-util');
var connect = require('gulp-connect');
var os = require('os');
var open = require('gulp-open');
var webpack = require('webpack');
var gulpWebpack = require('gulp-webpack');
var webpackDevConfig = require('./webpack.dev');
var webpackDistConfig = require('./webpack.dist');
var webpackDevServer = require('webpack-dev-server');


gulp.task('dev',['webpack-server','connect','watch'],function(){
  gulp.src(['app/index.html'])
      .pipe(open({uri:'http://localhost:8080'}))
})

gulp.task('build',function(){
  gulp.src('./wp-content/themes/eyasweb/src/app.js')
      .pipe(gulpWebpack(webpackDistConfig))
      .pipe(gulp.dest('./wp-content/themes/eyasweb/dist/'));
})

gulp.task('webpack-server',function(){
  var compiler = webpack(webpackDevConfig);
  new webpackDevServer(compiler,{
    contentBase:'./app/',
    publicPath:webpackDevConfig.output.publicPath,
    hot:true,
    quiet:true,
  }).listen(8888,'localhost',function(err){
    if(err) util.log(err);
  })
})

gulp.task('connect',function(){
  connect.server({
    root: './app/',
    port:8080,
    livereload: true
  });
})


gulp.task('html',function(){
  gulp.src(['./app/**/*.html'])
      .pipe(connect.reload());
})

gulp.task('watch',function(){
  gulp.watch(['./app/**/*.html','./app/**/*.css'],['html']);
})
